import Vue from 'vue'

const state = () => {
  return {
    videoUploadTasks: []
  }
}

const mutations = {
  ADD_VIDEO_TASKS: (state, task) => {
    state.videoUploadTasks.unshift(task)
  },
  SET_VIDEO_TASK: (state, data) => {
    const { index, task } = data
    const uploadTask = JSON.parse(JSON.stringify(state.videoUploadTasks))
    uploadTask[index] = task
    state.videoUploadTasks = uploadTask
    // Vue.set(state.videoUploadTasks, index, task)
  },
  REMOVE_SUCCESS_VIDEO_TASKS: (state, data) => {
    const task = state.videoUploadTasks.filter(item => item.state !== 'success')
    state.videoUploadTasks = task
  }
}

const actions = {
  addTask({ commit }, task) {
    commit('ADD_VIDEO_TASKS', task)
  },
  setTask({ commit }, data) {
    commit('SET_VIDEO_TASK', data)
  },
  removeSuccessTask({ commit }, task) {
    commit('REMOVE_SUCCESS_VIDEO_TASKS')
  }
}

const getters = {

}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
